package main

import (
	"fmt"
	"os"
	"io/ioutil"
	"encoding/xml"
)

//https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/07.1.md

/*
<?xml version="1.0" encoding="utf-8"?>
<servers version="1">
    <server>
        <serverName>Shanghai_VPN</serverName>
        <serverIP>127.0.0.1</serverIP>
    </server>
    <server>
        <serverName>Beijing_VPN</serverName>
        <serverIP>127.0.0.2</serverIP>
    </server>
</servers>
*/
type Recurlyservers struct{
	XMLName xml.Name `xml:"servers"`
	Version string   `xml:"version,attr"`
	Svs     []server `xml:"server"`
	Description string `xml:",innerxml"`
}
type server struct{
	XMLName xml.Name `xml:"server"`
	ServerName string `xml:"serverName"`
	ServerIP string `xml:"ServerIP"`
}

func main(){
	fmt.Println("start parse XML...")
	file,err:=os.Open("servers.xml")
	if err!=nil{
		fmt.Printf("erros: %v\n",err)
		return
	}
	
	defer file.Close()
	data,err:=ioutil.ReadAll(file)
	if err!=nil{
		fmt.Printf("error:%v\n",err)
		return
	}
	v:=Recurlyservers{}
	err=xml.Unmarshal(data,&v)
	if err!=nil{
		fmt.Printf("error:%v\n",err)
	}
	fmt.Println("----------start----------")
	fmt.Println(v)
	fmt.Println("---------- end ----------")
	fmt.Printf("Type of v : %T\n",v)
}